# -*- coding: utf-8 -*-
# @Time : 2021/9/9 14:41
# @Author : 阿怪
# @Email : scg@gmail.com
# @File : CRM-ST-TJFX-007.py
# @Project : crm自动化测试项目实战

'''验证线索总计的查看功能'''

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert  # 把弹窗封装成类Alert
driver = webdriver.Chrome()

# name_locator = (By.NAME,'name')
# passwd_locator = (By.NAME,'password')
# submit_locator = (By.NAME,'submit')
# driver.get('http://192.168.1.140/crm3/')
# driver.find_element(*name_locator).send_keys('admin')
# driver.find_element(*passwd_locator).send_keys('admin123')
# driver.find_element(*submit_locator).click()
# cookies = driver.get_cookies()
# cookie = {}
# for k,v in cookies[0].items():
#     if k == 'name' or k =='value':
#         cookie[k] = v
# print(cookie)

#（自动化的时候：先到统计页面查看共计数量，存下来，回到添加线索界面添加一条，再回到统计页面判断现在的共计数量是不是原来基础上＋1）
cookie = {'name': 'PHPSESSID', 'value': 'spv6hql2mag08vqgdmthnd2sv7'} # 获取到cookid
driver.get('http://192.168.1.140/')  # 访问服务器
driver.add_cookie(cookie)
driver.get('http://192.168.1.140/crm3/index.php?m=leads&a=index')
#已处于线索首页
try:
    clue_locator = (By.CSS_SELECTOR,'body > div.container > div.page-header > ul > li:nth-child(1) > a')  #线索
    clue_add_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > div > a')   #添加线索定位器
    clue_checkall_locator = (By.ID,'check_all') #全选线索定位器
    operation_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > ul > li:nth-child(1) > div > a')
     #批量操作定位器


    #先去添加线索数据
    driver.find_element(*clue_add_locator).click()
    driver.find_element(By.XPATH,'//*[@id="contacts_name"]').send_keys('欧哥')
    driver.find_element(By.NAME,'submit').click()
    #1.进入线索统计页面
    driver.get('http://192.168.1.140/crm3/index.php?m=leads&a=analytics')

    #每条用例都要清理数据。。。。回到线索界面把添加的线索全部删掉，有弹窗！害
    driver.find_element(*clue_locator).click()
    driver.find_element(*clue_checkall_locator).click()
    driver.find_element(*operation_locator).click()
    driver.find_element(By.XPATH,'//*[@id="delete"]').click()
    alert = Alert(driver)   #实例化Alert
    sleep(3)
    alert.accept()
    sleep(3)
    driver.quit()


except Exception as e:
    raise e
